<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>高级隔行变色</title>
		<style>
			table{
				width:500px;
				
				font-size:30px;
				margin:50px auto;
			}
			  th,
        td {
            border: 1px solid #d0d0d0;
            color: #404060;
            padding: 10px;
        }

        th {
            background-color: #09c;
            font: bold 16px "微软雅黑";
            color: #fff;
        }

        td {
            font: 14px "微软雅黑";
        }

        tbody tr {
            background-color: #f0f0f0;
            cursor: pointer;
        }
          table {
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #c0c0c0;
            width: 500px;
        }

		
		</style>
	</head>
	<body>
		<table >
			<thead>
			<tr >
				<th>序号</th>
				<th>姓名</th>
				<th>课程</th>
				<th>成绩</th>
			</tr>
			</thead>
			<tbody id="table">
			<tr >
				<td>1</td>
				<td>a</td>
				<td>b</td>
				<td>c</td>
			</tr>
			<tr>
				<td>2</td>
				<td>a</td>
				<td>b</td>
				<td>c</td>
			</tr>
			<tr>
				<td>3</td>
				<td>a</td>
				<td>b</td>
				<td>c</td>
			</tr>
			<tr >
				<td>3</td>
				<td>a</td>
				<td>b</td>
				<td>c</td>
			</tr>
			<tr >
				<td>3</td>
				<td>a</td>
				<td>b</td>
				<td>c</td>
			</tr>
			<tr >
				<td>3</td>
				<td>a</td>
				<td>b</td>
				<td>c</td>
			</tr>
			</tbody>
			
		</table>
		<script>
			
		var tr = document.getElementsByTagName("tr");
	
		 var color = "";
		 for(var i=0;i<tr.length;i++){
            if(i%2!==0){
                tr[i].style.backgroundColor = "#a3a3a3";
            }else{
                tr[i].style.backgroundColor = "#ccc";
            }

		
		tr[i].onmouseover=function ()
			{
				  color = this.style.backgroundColor;
				this.style.backgroundColor="#fff";
			}
		tr[i].onmouseout=function (){
				this.style.backgroundColor="color";
			}
		}
		/*	var tr = document.getElementsByTagName("tr");
        //循环判断并各行赋值属性（背景色）
        for(var i=0;i<tr.length;i++){
            if(i%2!==0){
                tr[i].style.backgroundColor = "#a3a3a3";
            }else{
                tr[i].style.backgroundColor = "#ccc";
            }

            //鼠标进入高亮显示
            //难点：鼠标移开的时候要回复原始颜色。
            //计数器（进入tr之后，立刻记录颜色，然后移开的时候使用记录好的颜色）
            var color = "";
            tr[i].onmouseover = function () {
                //赋值颜色之前，先记录颜色
                color = this.style.backgroundColor;
                this.style.backgroundColor = "#fff";
            }
            tr[i].onmouseout = function () {
                this.style.backgroundColor = color;
            }
        }*/

			
		</script>
	</body>
</html>
